home *** CD-ROM | disk | FTP | other *** search
/ Top 200 Programs / Top 200 Programs.iso / Bob8 / THOMPSON / LIBERTY / PRODUCT / TUTORIAL.EXE / WK2SOL.TXT < prev    next >
Text File  |  1996-12-10  |  4KB  |  123 lines

  1. Week 2 course material: Solutions to challenge exercises
  2. Liberty BASIC programming course
  3. Copyright 1996 Shoptalk Systems
  4. All Rights Reserved
  5.  
  6. -----------------------------------------------------------------------------
  7.  
  8. 1) Add code to ARRAYS.BAS after [quit] to display all the names entered.
  9.  
  10.     'ARRAYS.BAS
  11.     'List handling with arrays
  12.     'Here is a version that displays the names entered
  13.     'after [quit]
  14.  
  15.     dim names$(10)  'set up our array to contain 10 items
  16.  
  17. [askForName]  'ask for a name
  18.     input "Please give me your name ?"; yourName$
  19.     if yourName$ = "" then print "No name entered." : goto [quit]
  20.  
  21.     index = 0
  22. [insertLoop]
  23.     'check to see if index points to an unused item in the array
  24.     if names$(index) = "" then names$(index) = yourName$ : goto [nameAdded]
  25.     index = index + 1 'add 1 to index
  26.     if index < 10 then [insertLoop] 'loop back until we have counted to 10
  27.  
  28.     'There weren't any available slots, inform user
  29.     print "All ten name slots already used!"
  30.     goto [quit]
  31.  
  32. [nameAdded]  'Notify the name add was successful
  33.  
  34.     print yourName$; " has been added to the list."
  35.     goto [askForName]
  36.  
  37. [quit]
  38.  
  39.     'display all the entered names
  40.     print
  41.     print "Here is a list of the names entered:"
  42.     print "------------------------------------"
  43.     index = 0
  44.  
  45. [displayLoop]
  46.  
  47.     if names$(index) <> "" then print names$(index)
  48.     index = index + 1
  49.     if index < 10 then [displayLoop]
  50.  
  51.     end
  52.  
  53. -----------------------------------------------------------------------------
  54.  
  55. 2) Modify the AVERAGE.BAS program so it asks for a name and age for up to 20
  56. people.  The names should be managed in a string array (remember ARRAYS.BAS?)
  57. that you'll add to the program.  When the list of entries is displayed, each
  58. name is to be displayed with its age.  Then the total and average age will be
  59. displayed after this.  Call the program AGES.BAS.
  60.  
  61.  
  62.  
  63.     'AGES.BAS
  64.     'Accept some names and ages from the user, then total and average them
  65.     dim numbers(20)
  66.     dim names$(20)
  67.     print "AGES.BAS"
  68.     print
  69.  
  70.     'loop up to 20 times, getting names and ages
  71.     print "Enter up to 20 names with ages."
  72.     print "A blank name entry ends the series."
  73.  
  74. [entryLoop]  'loop around until a zero entry or until index = 20
  75.  
  76.     'get a name and age
  77.     print "Name "; index + 1;
  78.     input name$
  79.     if name$ = "" then [endSeries]  'quit if name$ is blank
  80.     print "Age   ";
  81.     input age
  82.  
  83.     index = index + 1       'add one to index
  84.     names$(index) = name$   'set the specified array item to be name$
  85.     numbers(index) = age    'set the specified array item to be age
  86.     total = total + age     'add entry to the total
  87.  
  88.     if index = 20 then [endSeries]  'if 20 values were entered, exit loop
  89.  
  90.     goto [entryLoop]  'go back and get another entry
  91.  
  92. [endSeries]  'entries are finished
  93.  
  94.     'Set entryCount to index
  95.     entryCount = index
  96.     if entryCount = 0 then print "No Entries." : goto [quit]
  97.  
  98.     print "Entries completed."
  99.     print
  100.     print "Here are the "; entryCount; " entries:"
  101.     print "-----------------------------"
  102.  
  103.     'This loop displays each entered value in turn.
  104.     'Notice that we re-use the index variable.  It
  105.     'can be confusing to use a new variable for each
  106.     'new loop.
  107.     index = 0
  108. [displayLoop]
  109.     index = index + 1
  110.     print "Entry "; index; " is "; names$(index); ", age "; numbers(index)
  111.     if index < entryCount then [displayLoop]
  112.  
  113.     'Now display the total and average value
  114.     print
  115.     print "The total age is "; total
  116.     print "The average age is "; total / entryCount
  117.  
  118. [quit]
  119.  
  120.     end
  121.  
  122.  
  123.